package theard;

/*
 * 线程同步问题
 */
public class BankTheard {

	
	public static void main(String[] args) throws InterruptedException {
		
		Bank bank =new Bank();
		
		Thread t1 =new MoneyThread(bank);  
		Thread t2 =new MoneyThread(bank);
		t1.start();
		t2.start();
		
		Thread.sleep(2000);
		
		System.out.println(bank.money);
	}
	
}



class Bank{
	
	public static int money = 1000 ;
	
	/**
	 * 取钱方法
	 * @param number   
	 * @return
	 */
	public synchronized int getMoney(int number){
		if(number<0){
			return -1;
		}else if(number > money){
			return -2;
		}else{
			try{
				Thread.sleep(1000);
			}catch(InterruptedException e){
				e.printStackTrace();
			}
			money -= number;
			return number;
		}
	}
	
}


class MoneyThread extends Thread{
	
	private Bank bank;
	
	public MoneyThread(Bank bank){
		this.bank =bank;
	}
	
	@Override
	public void run(){
		System.out.println(bank.getMoney(800));
	}
	
	
}









